﻿using System.Windows.Forms;
using SdmxMl.Structure;
using SdmxMl.Manager;

namespace SdmxMl.UC
{
    public partial class UcDataflow : UserControl
    {
        protected TreeNode treeNode;
        private DataFlowInsider _dataflowInsider;
        private ArtefactList alDsdRefList = null;

        public UcDataflow()
        {
            InitializeComponent();
            ucEditReference1.AtrefactType = SdmxArtefactType.KeyFamilies;
        }

        public virtual void BindFromNode(TreeNode node)
        {
            treeNode = node;
            _dataflowInsider = null;
            if (node != null)
            {
                _dataflowInsider = node.Tag as DataFlowInsider;

            }
            BindToControls();

        }

        private void BindToControls()
        {
            KeyFamilyType dsd = null;
            if (_dataflowInsider != null)
            {
                Visible = true;
                tabControlRef.SelectedIndex = 0;

                ucEditReference1.ArtefactRef = _dataflowInsider.DSDRef;
                alDsdRefList = ReferenceManager.GetArtefactList(SdmxArtefactType.KeyFamilies);
                dsd = ReferenceManager.LocateDsd(_dataflowInsider.DSDRef);


            }
            else
                Visible = false;
        }

        public bool UpdateNodeTag(TreeNode node)
        {
            bool err = false;
            if (node != null)
            {
                err = BindFromControls();
                if (_dataflowInsider != null && node.Text != _dataflowInsider.ToString())
                    node.Text = _dataflowInsider.ToString();
            }

            return err; // true on error
        }

        protected bool BindFromControls()
        {
            bool err = false;
            return err;
        }

        private void buttonNavigateScheme_Click_1(object sender, System.EventArgs e)
        {
            if (_dataflowInsider != null)
                if (_dataflowInsider.DSDRef != null)
                {
                    KeyFamilyType dsd =
                        ReferenceManager.LocateDsd(_dataflowInsider.DSDRef);
                    if (dsd != null)
                        NavigationManager.RequestNavigation(dsd);
                }

        }
    }
}
